using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;

namespace WeChat.Api.Common
{
    public static class JsonHelper
    {
        #region DataTable、JSON 互转
        public static DataTable jsonToTable(this string Json)
        {
            return Json == null ? null : JsonConvert.DeserializeObject<DataTable>(Json);
        }
        /// <summary>
        /// 数据表格转JSON
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string dtToJson(DataTable dt)
        {
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            return Newtonsoft.Json.JsonConvert.SerializeObject(dt, timeFormat);
        }
        #endregion

        #region 其他转换方式
        /// <summary>
        /// 对象转JSON
        /// </summary>
        /// <param name="Json"></param>
        /// <returns></returns>
        public static object ToJson(this string Json)
        {
            return Json == null ? null : JsonConvert.DeserializeObject(Json);
        }
        /// <summary>
        /// 将对象转换成JSON格式
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJson(this object obj)
        {
            var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            return JsonConvert.SerializeObject(obj, timeConverter);
        }
        public static T ToObject<T>(this string Json)
        {
            return Json == null ? default(T) : JsonConvert.DeserializeObject<T>(Json);
        }

        public static List<T> ToList<T>(this string Json)
        {
            return Json == null ? null : JsonConvert.DeserializeObject<List<T>>(Json);
        }

        public class ModelConvertHelper<T> where T : new()  // 此处一定要加上new()
        {
            //datatable to list
            public static List<T> ConvertToModel(DataTable dt)
            {

                List<T> ts = new List<T>();// 定义集合
                Type type = typeof(T); // 获得此模型的类型
                string tempName = "";
                foreach (DataRow dr in dt.Rows)
                {
                    T t = new T();
                    PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
                    foreach (PropertyInfo pi in propertys)
                    {
                        tempName = pi.Name;
                        if (dt.Columns.Contains(tempName))
                        {
                            if (!pi.CanWrite) continue;
                            object value = dr[tempName];
                            if (value != DBNull.Value)
                                pi.SetValue(t, value, null);
                        }
                    }
                    ts.Add(t);
                }
                return ts;
            }
        }
        #endregion

        public static string MsgBuild(int MsgID, string MsgBody, bool NeedAuthkey)
        {
            if (NeedAuthkey)
            {
                return "\"message\":" + MsgID + ",\"messagestr\":\"" + MsgBody + "\",\"authkey\":\"jiaozheng\"";
            }
            else
            {
                return "\"message\":" + MsgID + ",\"messagestr\":\"" + MsgBody + "\"";
            }
        }
        public static string EnityTojson(object model)
        {
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            return Newtonsoft.Json.JsonConvert.SerializeObject(model, timeFormat);
        }
    }
}
